package com.tuerks.sbahn;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class StationPickerActivity extends ListActivity {

	private String[][] stationList = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		startUpdate();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.emptymenu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		}
		return false;
	}

	private void showList() {
		String[] sl;
		sl = new String[stationList.length];
		for (int i = 0; i < stationList.length; i++) {
			sl[i] = stationList[i][0];
		}
		setListAdapter(new ArrayAdapter<String>(this,
				R.layout.stationpickeractivity, sl));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setFastScrollEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					addStationToPrefs(stationList[position][1]);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				// add to selected station
				// return
			}
		});

	}

	public void addStationToPrefs(String stationUrl) throws JSONException {
		JSONObject jO = new JSONObject();

		JSONArray jA = new JSONArray();

		// read prefs
		SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
				MODE_WORLD_READABLE);
		String stations = myPrefs.getString("stations", null);

		if (stations != null) {
			jO = new JSONObject(stations);
			jA = jO.getJSONArray("stations");
			jA.put(stationUrl);
			try {
				jO.put("stations", jA);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			jO = new JSONObject();
			jA.put(stationUrl);
			try {
				jO.put("stations", jA);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		SharedPreferences.Editor prefsEditor = myPrefs.edit();
		prefsEditor.putString("stations", jO.toString());
		prefsEditor.commit();
		startActivity(new Intent(this, MainActivity.class));
	}

	private class ProcessNetworkData extends AsyncTask<Void, Void, String[][]> {
		@Override
		protected String[][] doInBackground(Void... doesnotmatter) {

			try {
				stationList = SbahnUtil.getStations();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return stationList;
		}

		protected void onPostExecute(String[][] dhdhd) {
			try {
				showList();

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public void startUpdate() {
		ProcessNetworkData task = new ProcessNetworkData();
		task.execute();

	}
}
